<?php

namespace app\admin\controller;

use app\admin\model\BdDatacenter;
use app\admin\model\BdOrg;
use app\admin\model\Synlogs;
use app\common\controller\Backend;
use app\erp\controller\Bizperson;
use app\erp\controller\BizpersonCate;
use app\erp\controller\Carinfo;
use app\erp\controller\CarinfoCate;
use app\erp\controller\Customer;
use app\erp\controller\CustomerCate;
use app\erp\controller\Inventory;
use app\erp\controller\Mater;
use app\erp\controller\MaterCopy;
use app\erp\controller\MaterialCate;
use app\erp\controller\MaterialCateCopy;
use app\erp\controller\Matprop;
use app\erp\controller\MatpropCopy;
use app\erp\controller\Payrecord;
use app\erp\controller\Payrecordno;
use app\erp\controller\Purorder;
use app\erp\controller\Surface;
use app\erp\controller\SurfaceCate;
use app\erp\controller\Texture;
use app\erp\controller\TextureCate;
use think\Env;

class Synchronous extends Backend
{
    protected $action = [];
    public function _initialize()
    {
        parent::_initialize();
        $this->action = [
            'MaterialCate'                          => '物料_分组',
            'matprop'                               => '物料属性',
            'material'                              => '物料',

            'bizpersoncate'                         => '业务员_分组',
            'bizperson'                             => '业务员',

            'texturecate'                           => '材质_分组',
            'texture'                               => '材质',
            'surfacecate'                           => '表面_分组',
            'surface'                               => '表面',
            'carinfocate'                           => '车辆信息_分组',
            'carinfo'                               => '车辆信息',
            'curstomercate'                         => '客户_分组',
            'curstomer'                             => '客户',

            'ivnentory'                             => '即时库存',
            'purorder'                              => 'ERP订单',
            'payrecord'                             => '付款记录（已确认）',
            'payrecordno'                           => '付款记录（未通过）',
        ];
        $copy_action = [
            'MaterialCate'                          => '物料_分组',
            'matprop'                               => '物料属性',
            'material'                              => '物料',
        ];

        $this->assign('action',$this->action);
        $this->assign('copy_action',$copy_action);
    }
    public function index()
    {
        $data = BdDatacenter::select();

        return view('',[
            'data'  => $data,
            'herf'      => now_domain() . '/BdlLogin.php/sys/synlogs?ref=addtabs'
        ]);
    }

    public function action($id)
    {
        if($this->request->isPost())
        {
            set_time_limit(0);

            $params         = $this->request->post('row/a');
            $datacenter     = BdDatacenter::get($id);
            $forgs          = BdOrg::where('id','in',$params['forg'])->select();
            if($params['size'] > 2000)
            {
                $this->error('单次同步数据量不能大于2000');
            }
            if(empty($params['page']))
            {
                $this->error('请填写页码');
            }
            if(empty($params['start_time']))
            {
                $this->error('请选择开始时间');
            }
            if(empty($params['end_time']))
            {
                $this->error('请选择结束时间');
            }
            //判断密钥
            if(empty($datacenter['fk_bdl_clientsecret']))
            {
                $this->error('请先设置密钥');
            }

            $res    = $this->erp($params['action'],$params['start_time'], $params['end_time'], "bdl_WeChat", $datacenter['fk_bdl_clientsecret'],$datacenter['fnumber'], Env::get('erp.user_name'));
            $count  = [];
            foreach ($forgs as $k => $v)
            {
                $uuid = Synlogs::create([
                    'forgid'                => $v['id'],
                    'fk_bdl_bd_datacenter'  => $v['fk_bdl_bd_datacenter'],
                    'fk_bdl_username'       => session('admin')['nickname'],
                    'fk_bdl_calltime'       => date('Y-m-d H:i:s'),
                    'fk_bdl_executetype'    => 1,
                    'fk_bdl_callmethod'     => $this->action[$params['action']]
                ])->id;

                $run_res = $res->run($v['fnumber'],$v['id'],$params['page'],$params['size'],$uuid);

                $count[] = $run_res;
            }
            if(count($count) ===  count($forgs))
            {
                $this->error(date('Y-m-d H:i:s'). PHP_EOL . '同步成功,请继续同步其他数据,当前分页'. $params['page'] .
                "同步数据分别为：" . implode($count,',') . PHP_EOL,'','',10);
            }

            $this->error('无');
        }

        $data = BdDatacenter::get($id);
        //获取组织
        $forg = BdOrg::where('fk_bdl_bd_datacenter',$data['fnumber'])->select();

        return view('',[
            'forg'      => $forg,
            'data'      => $data
        ]);
    }


    public function copy($id)
    {

        if($this->request->isPost())
        {
            $params                 = $this->request->post('row/a');
            $form_datacenter        = BdDatacenter::where('fnumber',$params['fk_bdl_bd_datacenter'])->find();

            if($params['size'] > 2000)
            {
                return $this->error('单次同步数据量不能大于2000');
            }
             //判断密钥
//            if(empty($form_datacenter['fk_bdl_clientsecret']) || $form_datacenter['fk_bdl_clientsecret'] ==' ')
//            {
//                return $this->error('数据源-数据中心未设置密钥');
//            }

            $form_forgs             = BdOrg::where('id',$params['forgid'])->find();
//            ---

            $to_datacenter          = BdDatacenter::get($id);
            $to_forgs               = BdOrg::where('id',$params['forg'])->find();


            $res    = $this->erp_copy($params['action'],$params['start_time'], $params['end_time'], "bdl_WeChat", $form_datacenter['fk_bdl_clientsecret'],$form_datacenter['fnumber'], Env::get('erp.user_name'));

            $uuid = Synlogs::create([
                'forgid'                => $form_forgs['id'],
                'fk_bdl_bd_datacenter'  => $form_forgs['fk_bdl_bd_datacenter'],
                'fk_bdl_username'       => session('admin')['nickname'],
                'fk_bdl_calltime'       => date('Y-m-d H:i:s'),
                'fk_bdl_comment'        => $form_forgs['fk_bdl_bd_datacenter'] . ":__{$form_forgs['fname']}" . '拷贝给' . $to_forgs['fk_bdl_bd_datacenter'] . ":__{$form_forgs['fname']}",
                'fk_bdl_executetype'    => 1,
                'fk_bdl_callmethod'     => $this->action[$params['action']]
            ])->id;

            $run_res = $res->run($form_forgs['fnumber'],$form_forgs['id'],$params['page'],$params['size'],$to_datacenter,$to_forgs,$uuid);


            $this->error(date('Y-m-d H:i:s'). PHP_EOL . '同步成功,请继续同步其他数据,当前分页'. $params['page'] .
                "拷贝数据为：" . $run_res . PHP_EOL,'','',10);
        }

        $data = BdDatacenter::get($id);
        //获取组织
        $forg = BdOrg::where('fk_bdl_bd_datacenter',$data['fnumber'])->select();

        return view('',[
            'forg'      => $forg,
            'data'      => $data,
        ]);
    }

    public function erp($type,$starttime, $endtime,$client_id,$client_secret,$account_id,$user_name)
    {
        switch ($type)
        {
            case "MaterialCate":
                $erp = new MaterialCate($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "matprop":
                $erp = new Matprop($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "material":
                $erp = new Mater($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;

            case "bizpersoncate":
                $erp = new BizpersonCate($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "bizperson":
                $erp = new Bizperson($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "carinfo":
                $erp = new Carinfo($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "carinfocate":
                $erp = new CarinfoCate($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "texturecate":
                $erp = new TextureCate($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "texture":
                $erp = new Texture($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "surfacecate":
                $erp = new SurfaceCate($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "surface":
                $erp = new Surface($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "curstomercate":
                $erp = new CustomerCate($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "curstomer":
                $erp = new Customer($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;

            case "ivnentory":
                $erp = new Inventory($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;

            case "purorder":
                $erp = new Purorder($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "payrecord":
                $erp = new Payrecord($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "payrecordno":
                $erp = new Payrecordno($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;


        }

        return $erp;
    }
    public function erp_copy($type,$starttime, $endtime,$client_id,$client_secret,$account_id,$user_name)
    {
        switch ($type)
        {
            case "MaterialCate":
                $erp = new MaterialCateCopy($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "matprop":
                $erp = new MatpropCopy($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
            case "material":
                $erp = new MaterCopy($starttime, $endtime,$client_id,$client_secret,$account_id,$user_name);
                break;
        }

        return $erp;
    }
}